﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace TaiwuZD
{
    /// <summary>
    /// 技艺、功法
    /// 早熟 均衡 晚成
    /// </summary>
    public enum ChengShuXing
    {
        None,
        Early = 3,
        Normal = 2,
        Late = 4
    }

    /// <summary>
    /// 角色
    /// </summary>
    public class Role : BindableBase
    {

        public static ChengShuXing[] ChengShuXings { get; } = { ChengShuXing.Early, ChengShuXing.Normal, ChengShuXing.Late };


        private string _FirstName;
        private string _LastName;
        private int _Age;
        private int _Health;
        private int _MaxHealth;
        private int _Favor;
        private int _Degree;
        private int _InternalPower;
        private int _Like;
        private int _Abhor;
        private int _Charm;
        private int _GoodEvil;
        private int _Reputation;
        private ChengShuXing _JiyiJiaCheng;
        private ChengShuXing _GongfaJiaCheng;
        private int _BILI;
        private int _TIZHI;
        private int _LIMIN;
        private int _GENGGU;
        private int _WUXING;
        private int _DINGLI;

        private ObservableCollection<TwItem> _OwnItems = new ObservableCollection<TwItem>();
        private ObservableCollection<TwGongFa> _OwnedGongFas = new ObservableCollection<TwGongFa>();
        private ObservableCollection<TwSkill> _OwnedSkills = new ObservableCollection<TwSkill>();         

        public int Id { get; set; }

        /// <summary>
        /// 是否加载
        /// </summary>
        public bool IsLoaded { get; private set; }

        #region 基础属性
        /// <summary>
        /// 姓
        /// </summary> 
        public string FirstName
        {
            get { return _FirstName; }
            set { SetProperty(ref _FirstName, value); NotifyPropertyChanged(nameof(Name)); }
        }

        /// <summary>
        /// 名
        /// </summary>
        public string LastName
        {
            get { return _LastName; }
            set { SetProperty(ref _LastName, value); NotifyPropertyChanged(nameof(Name)); }
        }

        public string Name { get { return FirstName + LastName; } }

        public int Age
        {
            get { return _Age; }
            set { SetProperty(ref _Age, value); }
        }

        public int Health
        {
            get { return _Health; }
            set { SetProperty(ref _Health, value); }
        }

        public int MaxHealth
        {
            get { return _MaxHealth; }
            set { SetProperty(ref _MaxHealth, value); }
        }

        /// <summary>
        /// 好感度
        /// </summary>
        public int Favor
        {
            get { return _Favor; }
            set { SetProperty(ref _Favor, value); }
        }

        /// <summary>
        /// 精纯
        /// </summary>
        public int Degree
        {
            get { return _Degree; }
            set { SetProperty(ref _Degree, value); }
        }

        /// <summary>
        /// 内力
        /// </summary>
        public int InternalPower
        {
            get { return _InternalPower; }
            set { SetProperty(ref _InternalPower, value); }
        }

        /// <summary>
        /// 喜好
        /// </summary>
        public int Like
        {
            get { return _Like; }
            set { SetProperty(ref _Like, value); }
        }

        /// <summary>
        /// 厌恶
        /// </summary>
        public int Abhor
        {
            get { return _Abhor; }
            set { SetProperty(ref _Abhor, value); }
        }

        /// <summary>
        /// 魅力
        /// </summary>
        public int Charm
        {
            get { return _Charm; }
            set { SetProperty(ref _Charm, value); }
        }

        /// <summary>
        /// 善恶
        /// </summary>
        public int GoodEvil
        {
            get { return _GoodEvil; }
            set { SetProperty(ref _GoodEvil, value); }
        }

        /// <summary>
        /// 名誉
        /// </summary>
        public int Reputation
        {
            get { return _Reputation; }
            set { SetProperty(ref _Reputation, Math.Min(Math.Max(value, 0), 100)); }
        } 
        #endregion

        /// <summary>
        /// 技艺加成，早熟还是晚成
        /// </summary>
        public ChengShuXing JiyiJiaCheng
        {
            get { return _JiyiJiaCheng; }
            set { SetProperty(ref _JiyiJiaCheng, value); }
        }

        /// <summary>
        /// 功法加成，早熟还是晚成
        /// </summary>
        public ChengShuXing GongfaJiaCheng
        {
            get { return _GongfaJiaCheng; }
            set { SetProperty(ref _GongfaJiaCheng, value); }
        }

        #region 武力属性
        /// <summary>
        /// 臂力
        /// </summary>
        public int BILI
        {
            get { return _BILI; }
            set { SetProperty(ref _BILI, value); }
        }

        /// <summary>
        /// 体质
        /// </summary>
        public int TIZHI
        {
            get { return _TIZHI; }
            set { SetProperty(ref _TIZHI, value); }
        }

        /// <summary>
        /// 灵敏
        /// </summary>
        public int LIMIN
        {
            get { return _LIMIN; }
            set { SetProperty(ref _LIMIN, value); }
        }

        /// <summary>
        /// 根骨
        /// </summary>
        public int GENGGU
        {
            get { return _GENGGU; }
            set { SetProperty(ref _GENGGU, value); }
        }

        /// <summary>
        /// 悟性
        /// </summary>
        public int WUXING
        {
            get { return _WUXING; }
            set { SetProperty(ref _WUXING, value); }
        }

        /// <summary>
        /// 定力
        /// </summary>
        public int DINGLI
        {
            get { return _DINGLI; }
            set { SetProperty(ref _DINGLI, value); }
        }
        #endregion

        #region 技艺
        private int _JYQing;

        public int JYQing
        {
            get { return _JYQing; }
            set { SetProperty(ref _JYQing, value); }
        }

        private int _JYQi;

        public int JYQi
        {
            get { return _JYQi; }
            set { SetProperty(ref _JYQi, value); }
        }

        private int _JYShu;

        public int JYShu
        {
            get { return _JYShu; }
            set { SetProperty(ref _JYShu, value); }
        }

        private int _JYHua;

        public int JYHua
        {
            get { return _JYHua; }
            set { SetProperty(ref _JYHua, value); }
        }

        private int _JYShuShu;

        public int JYShuShu
        {
            get { return _JYShuShu; }
            set { SetProperty(ref _JYShuShu, value); }
        }

        private int _JYPingJian;

        public int JYPingJian
        {
            get { return _JYPingJian; }
            set { SetProperty(ref _JYPingJian, value); }
        }

        private int _JYDuanZao;

        public int JYDuanZao
        {
            get { return _JYDuanZao; }
            set { SetProperty(ref _JYDuanZao, value); }
        }

        private int _JYZhiMu;

        public int JYZhiMu
        {
            get { return _JYZhiMu; }
            set { SetProperty(ref _JYZhiMu, value); }
        }

        private int _JYYiShu;

        public int JYYiShu
        {
            get { return _JYYiShu; }
            set { SetProperty(ref _JYYiShu, value); }
        }




        private int _JYDuShu;

        public int JYDuShu
        {
            get { return _JYDuShu; }
            set { SetProperty(ref _JYDuShu, value); }
        }

        private int _JYZhiJing;

        public int JYZhiJing
        {
            get { return _JYZhiJing; }
            set { SetProperty(ref _JYZhiJing, value); }
        }

        private int _JYQiaoJiang;

        public int JYQiaoJiang
        {
            get { return _JYQiaoJiang; }
            set { SetProperty(ref _JYQiaoJiang, value); }
        }

        private int _JYDaoFa;

        public int JYDaoFa
        {
            get { return _JYDaoFa; }
            set { SetProperty(ref _JYDaoFa, value); }
        }

        private int _JYFoXue;

        public int JYFoXue
        {
            get { return _JYFoXue; }
            set { SetProperty(ref _JYFoXue, value); }
        }

        private int _JYChuYi;

        public int JYChuYi
        {
            get { return _JYChuYi; }
            set { SetProperty(ref _JYChuYi, value); }
        }

        private int _JYZaXue;

        public int JYZaXue
        {
            get { return _JYZaXue; }
            set { SetProperty(ref _JYZaXue, value); }
        }

        #endregion

        #region 武学

        private int _WXNeiGong;

        public int WXNeiGong
        {
            get { return _WXNeiGong; }
            set { SetProperty(ref _WXNeiGong, value); }
        }

        private int _WXShenFa;

        public int WXShenFa
        {
            get { return _WXShenFa; }
            set { SetProperty(ref _WXShenFa, value); }
        }


        private int _WXJueJi;

        public int WXJueJi
        {
            get { return _WXJueJi; }
            set { SetProperty(ref _WXJueJi, value); }
        }

        private int _WXZhangFa;

        public int WXZhangFa
        {
            get { return _WXZhangFa; }
            set { SetProperty(ref _WXZhangFa, value); }
        }


        private int _WXZhiFa;

        public int WXZhiFa
        {
            get { return _WXZhiFa; }
            set { SetProperty(ref _WXZhiFa, value); }
        }

        private int _WXTuiFa;

        public int WXTuiFa
        {
            get { return _WXTuiFa; }
            set { SetProperty(ref _WXTuiFa, value); }
        }

        private int _WXAnQi;

        public int WXAnQi
        {
            get { return _WXAnQi; }
            set { SetProperty(ref _WXAnQi, value); }
        }

        private int _WXJianFa;

        public int WXJianFa
        {
            get { return _WXJianFa; }
            set { SetProperty(ref _WXJianFa, value); }
        }

        private int _WXDaoFa;

        public int WXDaoFa
        {
            get { return _WXDaoFa; }
            set { SetProperty(ref _WXDaoFa, value); }
        }

        private int _WXChangBing;

        public int WXChangBing
        {
            get { return _WXChangBing; }
            set { SetProperty(ref _WXChangBing, value); }
        }

        private int _WXQiMen;

        public int WXQiMen
        {
            get { return _WXQiMen; }
            set { SetProperty(ref _WXQiMen, value); }
        }

        private int _WXRuanBing;

        public int WXRuanBing
        {
            get { return _WXRuanBing; }
            set { SetProperty(ref _WXRuanBing, value); }
        }

        private int _WXYuShe;

        public int WXYuShe
        {
            get { return _WXYuShe; }
            set { SetProperty(ref _WXYuShe, value); }
        }

        private int _WXYueQi;

        public int WXYueQi
        {
            get { return _WXYueQi; }
            set { SetProperty(ref _WXYueQi, value); }
        }


        #endregion

        #region 制造
        private int _Food;
        /// <summary>
        /// 食材
        /// </summary>
        public int Food
        {
            get { return _Food; }
            set { SetProperty(ref _Food, value); }
        }

        private int _Wood;
        /// <summary>
        /// 木材
        /// </summary>
        public int Wood
        {
            get { return _Wood; }
            set { SetProperty(ref _Wood, value); }
        }

        private int _Stone;
        /// <summary>
        /// 金石
        /// </summary>
        public int Stone
        {
            get { return _Stone; }
            set { SetProperty(ref _Stone, value); }
        }

        private int _Herb;
        /// <summary>
        /// 药材
        /// </summary>
        public int Herb
        {
            get { return _Herb; }
            set { SetProperty(ref _Herb, value); }
        }

        private int _Fabric;
        /// <summary>
        /// 织物
        /// </summary>
        public int Fabric
        {
            get { return _Fabric; }
            set { SetProperty(ref _Fabric, value); }
        }

        private int _Money;
        /// <summary>
        /// 银钱
        /// </summary>
        public int Money
        {
            get { return _Money; }
            set { SetProperty(ref _Money, value); }
        }


        private int _Prestige;
        /// <summary>
        /// 威望
        /// </summary>
        public int Prestige
        {
            get { return _Prestige; }
            set { SetProperty(ref _Prestige, value); }
        }
        #endregion

        /// <summary>
        /// 背包
        /// </summary>
        public ObservableCollection<TwItem> OwnedItems
        {
            get
            {
                if (_OwnItems.Count == 0 && Global.Inst.SD._actorItemsData.ContainsKey(Id))
                {
                    var itemsIndex = Global.Inst.SD._actorItemsData[Id];
                    var gallay = Global.Inst.SD._itemsData;

                    foreach (var item in itemsIndex)
                    {
                        if (gallay.ContainsKey(item.Key))
                        {
                            var iteminfo = gallay[item.Key];

                            var newitem = TwItem.Create(iteminfo);
                            newitem.Amount = itemsIndex[item.Key];
                            newitem.Key = item.Key;
                            _OwnItems.Add(newitem);
                        }
                        else
                        {
                            var newitem = new TwItem( item.Key);// TwItem.Create(item.Key, item.Value);
                            newitem.Amount = itemsIndex[item.Key];
                            _OwnItems.Add(newitem);
                        }
                    }

                }
                return _OwnItems;
            }
        }

        /// <summary>
        /// 特性
        /// </summary>
        public ObservableCollection<ActorFeature> RoleFeatures { get; private set; } = new ObservableCollection<ActorFeature>();


        public ObservableCollection<TwGongFa> OwnedGongFas
        {
            get
            {
                if (_OwnedGongFas.Count == 0 && Global.Inst.SD._actorGongFas.ContainsKey(Id))
                { 
                    var actorgf =  Global.Inst.SD._actorGongFas[Id]; 

                    foreach (var item in actorgf)
                    {
                        var id = item.Key;

                        var tpl = GameAssets.TwGongFas[id];

                        TwGongFa ngf = tpl.Clone() as TwGongFa;//new TwGongFa();
                        //ngf.SetData(101, id);
                        //ngf.SetData(0, tpl[0]);
                        //ngf.SetData(8, tpl[8]);
                        //ngf.SetData(99, tpl[99]);
                        //ngf.SetData(999, tpl[999]);
                        ngf.Proficiency = item.Value[0];
                        ngf.Degree = item.Value[1];
                        ngf.Reverse = item.Value[2];
                        

                        _OwnedGongFas.Add(ngf); 
                    }
                }
                return _OwnedGongFas;
            }
        }

        public ObservableCollection<TwSkill> OwnedSkills
        {
            get
            {
                if (_OwnedSkills.Count == 0)
                {
                    var actorgf = Global.Inst.SD._actorSkills;

                    foreach (var item in actorgf)
                    {
                        var id = item.Key;

                        var tpl = GameAssets.TwSkills[id];

                        TwSkill ngf = tpl.Clone() as TwSkill; 

                        ngf.Proficiency = item.Value[0];
                        //ngf.Degree = item.Value[1]; 
                        if (Global.Inst.SD._skillBookPages.ContainsKey(id))
                            ngf.BooksReaded = Global.Inst.SD._skillBookPages[id];

                        _OwnedSkills.Add(ngf);
                    }
                }
                return _OwnedSkills;
            }
        }

        public ObservableCollection<TwSkill> SkillBooksReaded
        {
            get
            {
                if (_OwnedSkills.Count == 0)
                {
                    var actorgf = Global.Inst.SD._actorSkills;

                    foreach (var item in actorgf)
                    {
                        var id = item.Key;

                        var tpl = GameAssets.TwSkills[id];

                        TwSkill ngf = tpl.Clone() as TwSkill;

                        ngf.Proficiency = item.Value[0];
                        ngf.Degree = item.Value[1];


                        _OwnedSkills.Add(ngf);
                    }
                }
                return _OwnedSkills;
            }
        }



        public string DebugInfo { get;private set; }


        public void LoadData()
        {
            if (IsLoaded) return;

            var actor = Global.Inst.SD._actorsData[Id];

            var aks = actor.Keys.OrderBy(x => x).Where(x=>x<11||x==17||x>19).Where(x=>x<60||x>66).Where(x=>x<501||x>613)
                .Where(x=>x<401||x>407).Where(x=>x!=0&&x!=551&&x!=651&&x!=706&&x!=3&&x!=5&&x!=29&&x!=202&&x!=203&&x!=101).ToArray();
            for (int i = 0; i < aks.Length; i++)
            {
                DebugInfo += $"{aks[i]:######}:{actor[aks[i]]:00000000}\t\t\t";
                if (i % 4 == 3)
                    DebugInfo += "\n";
            }

            if (actor.ContainsKey(0))
                LastName = actor[0];

            if (actor.ContainsKey(5))//主角
            {
                FirstName = actor[5];
            }
            else
            {
                int key = 0;
                if (int.TryParse(actor[29],out key))
                    FirstName = GameAssets.ActorSurNames[key];
            }

            if (actor.ContainsKey(3))//好感
                if (int.TryParse(actor[3], out _Favor))
                    NotifyPropertyChanged(nameof(Favor)); 

            if (actor.ContainsKey(11))
                if (int.TryParse(actor[11], out _Age))
                    NotifyPropertyChanged(nameof(Age));

            if (actor.ContainsKey(12))
                if (int.TryParse(actor[12], out _Health))
                    NotifyPropertyChanged(nameof(Health));

            if (actor.ContainsKey(13))
                if (int.TryParse(actor[13], out _MaxHealth))
                    NotifyPropertyChanged(nameof(MaxHealth));

            //魅力
            if (actor.ContainsKey(15))
                if (int.TryParse(actor[15], out _Charm))
                    NotifyPropertyChanged(nameof(Charm));

            //善恶
            if (actor.ContainsKey(16))
                if (int.TryParse(actor[16], out _GoodEvil))
                    NotifyPropertyChanged(nameof(GoodEvil));

            //声望
            if (actor.ContainsKey(18))
                if (int.TryParse(actor[18], out _Reputation))
                    NotifyPropertyChanged(nameof(Reputation));
             

            //技艺成熟
            if (actor.ContainsKey(551))
            {
                JiyiJiaCheng = (ChengShuXing)int.Parse(actor[551]); 
            } 

            //功法成熟早晚
            if (actor.ContainsKey(651))
            {
                GongfaJiaCheng = (ChengShuXing)int.Parse(actor[651]); 
            } 

            //精纯
            if (actor.ContainsKey(901))
                if (int.TryParse(actor[901], out _Degree))
                    NotifyPropertyChanged(nameof(Degree));

            //内力
            if (actor.ContainsKey(706))
                if (int.TryParse(actor[706], out _InternalPower))
                    NotifyPropertyChanged(nameof(InternalPower));

            //臂力
            if (actor.ContainsKey(61))
                if (int.TryParse(actor[61], out _BILI))
                    NotifyPropertyChanged(nameof(BILI));
            //体质
            if (actor.ContainsKey(62))
                if (int.TryParse(actor[62], out _TIZHI))
                    NotifyPropertyChanged(nameof(TIZHI));
            //灵敏
            if (actor.ContainsKey(63))
                if (int.TryParse(actor[63], out _LIMIN))
                    NotifyPropertyChanged(nameof(LIMIN));
            //根骨
            if (actor.ContainsKey(64))
                if (int.TryParse(actor[64], out _GENGGU))
                    NotifyPropertyChanged(nameof(GENGGU));
            //悟性
            if (actor.ContainsKey(65))
                if (int.TryParse(actor[65], out _WUXING))
                    NotifyPropertyChanged(nameof(WUXING));
            //定力
            if (actor.ContainsKey(66))
                if (int.TryParse(actor[66], out _DINGLI))
                    NotifyPropertyChanged(nameof(DINGLI));

            //特性
            if (actor.ContainsKey(101))
            {
                var tx = actor[101];

                RoleFeatures.Clear();
                string[] txarr = tx.Split(new char[1]
                {
                '|'
                }, StringSplitOptions.RemoveEmptyEntries);

                foreach (var item in txarr)
                {
                    var txf = GameAssets.ActorFeatures.FirstOrDefault(x => x.Id.ToString() == item);
                    if (txf != null)
                        RoleFeatures.Add(txf);
                }
            }
            
            //喜恶
            if (actor.ContainsKey(202))
                if (int.TryParse(actor[202], out _Like))
                    Like = GameAssets.TwPersonalInterests.Length > _Like ? _Like : 0; 

            if (actor.ContainsKey(203))
                if (int.TryParse(actor[203], out _Abhor))
                    Abhor = GameAssets.TwPersonalInterests.Length > _Abhor ? _Abhor : 0;


            if (actor.ContainsKey(401))
                if (int.TryParse(actor[401], out _Food))
                    NotifyPropertyChanged(nameof(Food));

            if (actor.ContainsKey(402))
                if (int.TryParse(actor[402], out _Wood))
                    NotifyPropertyChanged(nameof(Wood)); 

            if (actor.ContainsKey(403))
                if (int.TryParse(actor[403], out _Stone))
                    NotifyPropertyChanged(nameof(Stone));
             
            if (actor.ContainsKey(404))
                if (int.TryParse(actor[404], out _Fabric))
                    NotifyPropertyChanged(nameof(Fabric)); 

            if (actor.ContainsKey(405))
                if (int.TryParse(actor[405], out _Herb))
                    NotifyPropertyChanged(nameof(Herb));
          
            if (actor.ContainsKey(406))
                if (int.TryParse(actor[406], out _Money))
                    NotifyPropertyChanged(nameof(Money));

            if (actor.ContainsKey(407))
                if (int.TryParse(actor[407], out _Prestige))
                    NotifyPropertyChanged(nameof(Prestige));

            JYQing = int.Parse(actor[501]);
            JYQi = int.Parse(actor[502]);
            JYShu = int.Parse(actor[503]);
            JYHua = int.Parse(actor[504]);
            JYShuShu = int.Parse(actor[505]);
            JYPingJian = int.Parse(actor[506]);
            JYDuanZao = int.Parse(actor[507]);
            JYZhiMu = int.Parse(actor[508]);
            JYYiShu = int.Parse(actor[509]);
            JYDuShu = int.Parse(actor[510]);
            JYZhiJing = int.Parse(actor[511]);
            JYQiaoJiang = int.Parse(actor[512]);
            JYDaoFa = int.Parse(actor[513]);
            JYFoXue = int.Parse(actor[514]);
            JYChuYi = int.Parse(actor[515]);
            JYZaXue = int.Parse(actor[516]);
            WXNeiGong = int.Parse(actor[601]);
            WXShenFa = int.Parse(actor[602]);
            WXJueJi = int.Parse(actor[603]);
            WXZhangFa = int.Parse(actor[604]);
            WXZhiFa = int.Parse(actor[605]);
            WXTuiFa = int.Parse(actor[606]);
            WXAnQi = int.Parse(actor[607]);
            WXJianFa = int.Parse(actor[608]);
            WXDaoFa = int.Parse(actor[609]);
            WXChangBing = int.Parse(actor[610]);
            WXQiMen = int.Parse(actor[611]);
            WXRuanBing = int.Parse(actor[612]);
            WXYuShe = int.Parse(actor[613]);
            WXYueQi = int.Parse(actor[614]);

            IsLoaded = true;
        }

        private void SaveAttribute()
        {
            var actor = Global.Inst.SD._actorsData[Id];

            actor[0] = LastName;
            if (actor.ContainsKey(5))
            {
                actor[5] = FirstName;
            }
            else
            {
                var kv = GameAssets.ActorSurNames.FirstOrDefault(x => x.Value == FirstName);

                if (kv.Equals(new KeyValuePair<int, string>(0, null)))
                {
                    actor.Add(5, FirstName);
                }
                else
                {
                    if (!actor.ContainsKey(29))
                        actor.Add(29, kv.Key.ToString());
                    else
                        actor[29] = kv.Key.ToString();
                }
            }

            ///好感度
            if (actor.ContainsKey(3))
                actor[3] = Favor.ToString();
            else if (Favor != 0)
                actor.Add(3, Favor.ToString());


            actor[11] = Age.ToString();
            actor[12] = Health.ToString();
            actor[13] = MaxHealth.ToString();
            if (actor.ContainsKey(15))
            {
                actor[15] = Charm.ToString();
            }
            else if (Charm > 0)
            {
                actor.Add(15, Charm.ToString());
            }
            actor[16] = GoodEvil.ToString();

            if (actor.ContainsKey(651))
            {
                actor[651] = ((int)GongfaJiaCheng).ToString();
            }
            if (actor.ContainsKey(18))
            {
                actor[18] = Reputation.ToString();
            }
            if (actor.ContainsKey(551))
            {
                actor[551] = ((int)JiyiJiaCheng).ToString();
            }

            if (actor.ContainsKey(901))
            {
                actor[901] = Degree.ToString();
            }
            if (actor.ContainsKey(706))
            {
                actor[706] = InternalPower.ToString();
            }

            //喜好
            if (actor.ContainsKey(202))
                actor[202] = Like.ToString();
            else
                actor.Add(202, Like.ToString());

            if (actor.ContainsKey(203))
                actor[203] = Abhor.ToString();
            else
                actor.Add(203, Abhor.ToString());

            //if (actor.ContainsKey(995))
            //{
            //    actor[995] = dt_995.Text;
            //}
            //if (actor.ContainsKey(996))
            //{
            //    actor[996] = dt_996.Text;
            //}


            actor[61] = BILI.ToString();
            actor[62] = TIZHI.ToString();
            actor[63] = LIMIN.ToString();
            actor[64] = GENGGU.ToString();
            actor[65] = WUXING.ToString();
            actor[66] = DINGLI.ToString();


            actor[101] = "0|" + string.Join("|", RoleFeatures.Select(x => x.Id));

            actor[401] = Food.ToString();
            actor[402] = Wood.ToString();
            actor[403] = Stone.ToString();
            actor[404] = Fabric.ToString();
            actor[405] = Herb.ToString();
            actor[406] = Money.ToString();
            actor[407] = Prestige.ToString();

            actor[501] = JYQing.ToString();
            actor[502] = JYQi.ToString();
            actor[503] = JYShu.ToString();
            actor[504] = JYHua.ToString();
            actor[505] = JYShuShu.ToString();
            actor[506] = JYPingJian.ToString();
            actor[507] = JYDuanZao.ToString();
            actor[508] = JYZhiMu.ToString();
            actor[509] = JYYiShu.ToString();
            actor[510] = JYDuShu.ToString();
            actor[511] = JYZhiJing.ToString();
            actor[512] = JYQiaoJiang.ToString();
            actor[513] = JYDaoFa.ToString();
            actor[514] = JYFoXue.ToString();
            actor[515] = JYChuYi.ToString();
            actor[516] = JYZaXue.ToString();
            actor[601] = WXNeiGong.ToString();
            actor[602] = WXShenFa.ToString();
            actor[603] = WXJueJi.ToString();
            actor[604] = WXZhangFa.ToString();
            actor[605] = WXZhiFa.ToString();
            actor[606] = WXTuiFa.ToString();
            actor[607] = WXAnQi.ToString();
            actor[608] = WXJianFa.ToString();
            actor[609] = WXDaoFa.ToString();
            actor[610] = WXChangBing.ToString();
            actor[611] = WXQiMen.ToString();
            actor[612] = WXRuanBing.ToString();
            actor[613] = WXYuShe.ToString();
            actor[614] = WXYueQi.ToString();

        }

        /// <summary>
        /// 背包
        /// </summary>
        private void SaveBag()
        {
            if (!Global.Inst.SD._actorItemsData.ContainsKey(Id))
                Global.Inst.SD._actorItemsData.Add(Id, new Dictionary<int, int>());

            var actorItemsIndex = Global.Inst.SD._actorItemsData[Id];
            var actorItems = Global.Inst.SD._itemsData;

            var all = actorItemsIndex.Keys.Union(OwnedItems.Select(x => x.Key)).ToArray();

            foreach (var key in all)
            {
                var oi = OwnedItems.FirstOrDefault(x => x.Key == key);
                //添加
                if (oi != null && !actorItemsIndex.ContainsKey(key))
                {
                    if (oi[6] == "0")
                    {
                        AddNewItem(oi, ref actorItemsIndex, ref actorItems);
                    }
                    else
                    {
                        if (actorItemsIndex.ContainsKey(key))
                            actorItemsIndex[key] += oi.Amount;
                        else
                            actorItemsIndex.Add(key, oi.Amount);
                    }
                }//删除
                else if (oi == null && actorItemsIndex.ContainsKey(key))
                {
                    actorItemsIndex.Remove(key);
                    actorItems.Remove(key);
                }
                else//变更数量
                {
                    actorItemsIndex[key] = oi.Amount;
                }
            }

        }

        private void AddNewItem(TwItem oi, ref Dictionary<int, int> actorItemsIndex, ref Dictionary<int, Dictionary<int, string>> actorItems, bool bBookFull = true)
        {
            var key = actorItems.Max(x => x.Key) + 1;

            var newitem = oi.Clone() as TwItem;
            newitem.Key = key; 


            actorItemsIndex.Add(key, 1);
            actorItems.Add(key, oi.GetSaveInfo());
        }

        public void SaveData()
        {
            SaveAttribute();
            SaveBag();
            SaveGongFa();
            SaveSkills();
        }

        private void SaveGongFa()
        {
            if (!Global.Inst.SD._actorGongFas.ContainsKey(Id))
                Global.Inst.SD._actorGongFas.Add(Id, new SortedDictionary<int, int[]>());

            var actorgf = Global.Inst.SD._actorGongFas[Id];
            var union = actorgf.Keys.Union(OwnedGongFas.Select(x => x.Id)).ToArray();

            foreach (var key in union)
            {
                if (!actorgf.ContainsKey(key))
                {
                    var gf = OwnedGongFas.FirstOrDefault(x => x.Id == key);
                    actorgf.Add(key, new int[] { gf.Proficiency, gf.Degree, gf.Reverse });
                }
                else if (OwnedGongFas.All(x => x.Id != key))
                    actorgf.Remove(key);
                else
                {
                    var gf = OwnedGongFas.FirstOrDefault(x => x.Id == key);
                    actorgf[key][0] = gf.Proficiency;
                    actorgf[key][1] = gf.Degree;
                    actorgf[key][2] = gf.Reverse;
                }
            }

        }

        private void SaveSkills()
        {
            var actorgf = Global.Inst.SD._actorSkills;
            var union = actorgf.Keys.Union(OwnedSkills.Select(x => x.Id)).ToArray();

            foreach (var key in union)
            {
                if (!actorgf.ContainsKey(key))
                {
                    var gf = OwnedSkills.FirstOrDefault(x => x.Id == key);
                    actorgf.Add(key, new int[] { gf.Proficiency, gf.Degree });
                }
                else if (OwnedSkills.All(x => x.Id != key))
                    actorgf.Remove(key);
                else
                {
                    var gf = OwnedSkills.FirstOrDefault(x => x.Id == key);
                    actorgf[key][0] = gf.Proficiency;
                    actorgf[key][1] = gf.Degree; 
                }
            }

        }

        

    }




}
